/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import DAO.MatriculaDAO;
import DAO.PedidoDAO;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Mensagem;
import java.sql.SQLException;
import model.Pedido;

/**
 *
 * @author ASR Consultoria
 */
public class Controller_pedido extends HttpServlet {

    public void criar(HttpServletRequest request, HttpServletResponse response)            
            throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ArrayList<Object> obj = new ArrayList<Object>();
        Mensagem msg = new Mensagem();
        Gson gson = new Gson();

        Pedido pedido = new Pedido();
        PedidoDAO dao = new PedidoDAO();
        //MatriculaDAO matriculaDAO = new MatriculaDAO();
        
        try {
            //pedido.setMatricula(matriculaDAO.buscarByMatricula(Integer.parseInt(request.getParameter("nome"))));
            
            dao.criar(pedido);

          //  msg.setMensagem("Pedido criado com sucesso!!");

            obj.add(msg);

            out.print(gson.toJson(obj));

        } catch (Exception e) {
        //    msg.setMensagem("Erro ao cadastrar");
            System.out.println("Erro na servlet " + e.getStackTrace());

        } finally {
        }

    }

    public void listar(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ArrayList<Pedido> obj = new ArrayList<Pedido>();

        Mensagem objmsg = new Mensagem();

        Gson objJson = new Gson();

        try {
            PedidoDAO pedidoDAO = new PedidoDAO();

            obj = pedidoDAO.buscarPorData(Date.valueOf(request.getParameter("data_inicial")), Date.valueOf(request.getParameter("data_final")));

            out.print(objJson.toJson(obj));

        } catch (Exception e) {

            objmsg.setMensagem("Algo não previsto aconteceu, favor verificar se o Cliente foi cadastrado!");

            out.print(objJson.toJson(objmsg));

        } finally {
            out.close();
        }

    }

  
    public void buscar_por_id(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        PrintWriter out = response.getWriter();
        Gson objJson = new Gson();
        PedidoDAO dao = new PedidoDAO();

        ArrayList<Pedido> arrobj = new ArrayList<Pedido>();

        try {

            arrobj.add(dao.buscarLoginByCodigo(Integer.parseInt(request.getParameter("id"))));
            out.print(objJson.toJson(arrobj));

        } catch (Exception e) {
            System.out.println("Erro ao buscar por ID o login");
        } finally {
            out.close();
        }

    }

    public void apagar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        PrintWriter out = response.getWriter();
        Gson objJson = new Gson();
        PedidoDAO dao = new PedidoDAO();

        Mensagem msg = new Mensagem();

        ArrayList<Object> arrobj = new ArrayList<Object>();

        try {

            dao.marcarInativo(Integer.parseInt(request.getParameter("id")));
            msg.setMensagem("Registro apagado com sucesso!!");
            arrobj.add(msg);
            out.print(objJson.toJson(arrobj));

        } catch (Exception e) {
            System.out.println("Erro ao buscar por ID o login");
        } finally {
            out.close();
        }

    }
    
    public void finalizarPedido(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        PrintWriter out = response.getWriter();
        Gson objJson = new Gson();
        PedidoDAO dao = new PedidoDAO();

        Mensagem msg = new Mensagem();

        ArrayList<Object> arrobj = new ArrayList<Object>();

        try {

            dao.finalizarCompra(Integer.parseInt(request.getParameter("matricula")));
            msg.setMensagem("Compra efetuada com sucesso!!");
            arrobj.add(msg);
            out.print(objJson.toJson(arrobj));

        } catch (Exception e) {
            System.out.println("Erro ao finalizar a compra");
        } finally {
            out.close();
        }

    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int oper = Integer.parseInt(request.getParameter("oper"));
        try {
            switch (oper) {

                case 1:
                    criar(request, response);
                    break;

                case 2:
                    listar(request, response);
                    break;

               case 3:
                    finalizarPedido(request, response);
                    break;

                 /*     case 4:
                 consultar(request, response);
                 break;*/
                case 5:
                    apagar(request, response);
                    break;
                case 6:
                    buscar_por_id(request, response);
                    break;
            }

        } catch (Exception e) {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
